#include "header.h"
#include "config.h"
#include "extern.h"


int config_load (const char* fconf)
{
 char line[256];
 FILE* cf = NULL;

	cf = fopen (fconf, "r");
		if (cf == NULL)
		{
			printf ("unable to open %s\n", fconf);/*<------------------------------------*/
			return -1;
		}

	while (fgets (line, sizeof (line), cf) != NULL)
	{
		if (line[0] != '#') /* if the line isn't a comment */
		{
			if (!sscanf (line, "port=%u", &NetworkConf.Port))
			if (!sscanf (line, "sslport=%d", &NetworkConf.SslPort))
			if (!sscanf (line, "maxqueulen=%d", &NetworkConf.MaxQueuLen))
			if (!sscanf (line, "maxclones=%d", &NetworkConf.MaxClones))
			if (!sscanf (line, "mysqlhost=%s", MysqlConf.Host))
			if (!sscanf (line, "mysqluser=%s", MysqlConf.User))
			if (!sscanf (line, "mysqlpwd=%s", MysqlConf.Pwd))
			if (!sscanf (line, "mysqldb=%s", MysqlConf.DB))
			if (!sscanf (line, "mysqlport=%u", &MysqlConf.Port))
			if (!sscanf (line, "maxthreads=%d", &ServerConf.MaxThreads))
			if (!sscanf (line, "maxclients=%d", &ServerConf.MaxClients))
			if (!sscanf (line, "maxchannels=%d", &ServerConf.MaxChannels))
			if (!sscanf (line, "maxgamesperuser=%d", &ServerConf.MaxGamesPerUser))
			if (!sscanf (line, "EnableSysDebug=%d", &ServerConf.EnableSysDebug))
			if (!sscanf (line, "EnableMemDebug=%d", &ServerConf.EnableMemDebug))
			if (!sscanf (line, "pollthreads=%d", &ServerConf.PollThreads))
			if (!sscanf (line, "auththreads=%d", &ServerConf.AuthThreads))
				continue;
		}
	}

	fclose (cf);

 return 0;
}

void config_print ()
{
	printf ("##### CONFIG #####\n");
	printf ("port=%u\n",NetworkConf.Port);
	printf ("maxqueulen=%d\n",NetworkConf.MaxQueuLen);
	printf ("maxclones=%d\n",NetworkConf.MaxClones);
	printf ("mysqlhost=%s\n",MysqlConf.Host);
	printf ("mysqluser=%s\n",MysqlConf.User);
	printf ("mysqlpwd=%s\n",MysqlConf.Pwd);
	printf ("mysqldb=%s\n",MysqlConf.DB);
	printf ("mysqlport=%u\n",MysqlConf.Port);
	printf ("MaxThreads=%d\n",ServerConf.MaxThreads);
	printf ("auththreads=%d\n", ServerConf.AuthThreads);
	printf ("pollthreads=%d\n", ServerConf.PollThreads);
	printf ("maxclients=%d\n",ServerConf.MaxClients);
	printf ("maxchannels=%d\n",ServerConf.MaxChannels);
	printf ("maxgamesperuser=%d\n",ServerConf.MaxGamesPerUser);
	printf ("EnableSysDebug=%d\n",ServerConf.EnableSysDebug);
	printf ("EnableMemDebug=%d\n",ServerConf.EnableMemDebug);
	printf ("##### END OF CONFIG #####\n");
}

